(function () {
    //查询
    jQuery('.app-btn-search').on('click',function(){
        serverData.key=jQuery("#search").val();
        serverTable.draw();
    });
    //撤销查询
    jQuery('.app-btn-cancel').on('click',function(){
        jQuery('#search').val('');
        serverData.key='';
        serverTable.draw();
    });
	
    var serverData = {};
    var serverTable;
    jQuery(document).ready(function () {
        serverTable = jQuery('#dataTable').DataTable({
            "serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url":window.rootPath+ '/ui/kitchenwaste/getpage',
                "data": function (d) {
                    return $.extend({}, d, serverData);
                }
            },
            columns: [
                {
                data: null
                },
                {
                    data: null,
                    defaultContent: "",
                    render: function (data, type, full, meta) {
                        var operate ='<button type="button" class="btn btn-sm btn-info searchBtn" title="查看详情"><i class="fa fa-search"></i></button>';
                        return operate;

                    }
                },
                {
                    data: 'waste'
                },
                {
                    data: 'wasteOil'
                },
                {
                    data: 'remark'
                },
                {
                    data: 'businesssellerBaseInfo.shopName'
                },
                {
                    data: 'checkPerson.name'
                },
                {
                    data: 'createTime',
                    render: function (data, type, full, meta) {
                        if (data)
                            return moment(data).format('YYYY/MM/DD HH:mm');
                        else
                            return '';
                    }
                }  
              ],
				"drawCallback" : function(oSettings) {
					var k = 0;
					var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;
					for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
						$('td:eq(0)',oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
						k++;
					}

	                serverTable.off('click', 'tr');
	                serverTable.on('click', 'tr', function (event) {
		                    event = event || window.event;
		                    var target = event.target || event.scrElement;
		                    var data = serverTable.row(this).data();
		                    if (target.className.indexOf('fa-search') != -1 || target.className.indexOf('searchBtn') != -1) {
		                    		//alert(data);
		                        showDetail(data);
		                    }
	
		                    return;
	                });
				}
        });
        
        var showDetail=function(data){
        	if(!data) return;
        	var picUrl = data.picAddr;
        	var waste = data.waste
        	var wasteOil = data.wasteOil
    		if(picUrl==undefined||picUrl==""){
    			picUrl=window.rootPath + "/img/nopicture.png";
		}else{
			picUrl=window.rootPath+'/'+picUrl;
		}
        	if(waste==undefined||waste==""){
        		waste="";
        	}else{
        		waste+="kg";
        	}
        	if(wasteOil==undefined||wasteOil==""){
        		wasteOil="";
        	}else{
        		wasteOil+="kg";
        	}
        		
        		
        	jQuery('#shopName').html(data.businesssellerBaseInfo.shopName);
        	jQuery('#checkPerson').html(data.checkPerson.name);
        	jQuery('#createTime').html(moment(data.createTime).format('YYYY/MM/DD HH:mm'));
        	jQuery('#waste').html(waste);
        	jQuery('#wasteOil').html(wasteOil);
        	jQuery('#remark').val(data.remark);
        	jQuery('#avatar').attr("src",picUrl);
	
    		jQuery('#checkModal').modal('show');
        }
    });
    
})();